package com.cfw.prototype;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;

import org.w3c.dom.Node;

import ufw.collection.MapTree;
import ufw.prototype.TreeSearch.AbstractNavSourceCallback;
import ufw.prototype.TreeSearch.AbstractNavTargetCallback;

/**
 * 대신 com.cfw.collection.TreeSearchBaseCallback을 사용할 수 있음
 * <br/><b>History</b><br/>
 * <pre>
 * 2014. 7. 29. 최초작성
 * </pre>
 * @author 박형일
 * @version 1.0
 */
@Deprecated
public class TreeSearchBaseCallback {
	
	public static class MutableTreeNodeSource extends AbstractNavSourceCallback {
		public MutableTreeNodeSource(Object data) { super(data); }

		@Override
		public boolean isLeaf(Object node) { return ((DefaultMutableTreeNode)node).isLeaf(); }

		@Override
		public Object getNextSibling(Object node) { return ((DefaultMutableTreeNode)node).getNextSibling(); }

		@Override
		public Object getUserObject(Object node) { return ((DefaultMutableTreeNode)node).getUserObject(); }

		@Override
		public Object getFirstChild(Object node) { return ((DefaultMutableTreeNode)node).getFirstChild(); }
	}

	public static class MapTreeSource extends AbstractNavSourceCallback {
		public MapTreeSource(Object data) { super(data); }

		@Override
		public boolean isLeaf(Object node) { return ((MapTree)node).isLeaf(); }

		@Override
		public Object getNextSibling(Object node) { return ((MapTree)node).getNextSibling(); }

		@Override
		public Object getUserObject(Object node) { return ((MapTree)node).getUserObject(); }

		@Override
		public Object getFirstChild(Object node) { return ((MapTree)node).getFirstChild(); }
	}
	
	public static class XmlSource extends AbstractNavSourceCallback {
		public XmlSource(Object data) { super(data); }

		@Override
		public boolean isLeaf(Object node) { return !((Node)node).hasChildNodes(); }

		@Override
		public Object getNextSibling(Object node) { return ((Node)node).getNextSibling(); }

		@Override
		public Object getUserObject(Object node) { return node; }

		@Override
		public Object getFirstChild(Object node) { return ((Node)node).getFirstChild(); }
	}
	
	@SuppressWarnings("unchecked")
	public static class MapTarget extends AbstractNavTargetCallback {

		public MapTarget(Object data) { super(data); }

		@Override
		public void onroot(Object rootObject, Object userObject) {
			Map<String, Object> root = (Map<String, Object>)rootObject;
			Map<String, Object> data = (Map<String, Object>)userObject;
			
			root.put("@key", (String)data.get("key"));
			root.put("@value", (String)data.get("value"));
		}
		
		@Override
		public Object onnode(Object parentObject, Object userObject) {
			Map<String, Object> parent = (Map<String, Object>)parentObject;
			Map<String, Object> data = (Map<String, Object>)userObject;
			
			Map<String, Object> child = new HashMap<String, Object>();
			child.put("@key", (String)data.get("key"));
			child.put("@value", (String)data.get("value"));
			
			((List<Map<String, Object>>)parent.get("child")).add(child);
			return child;
		}

		@Override
		public void onchild(Object parent) {
			Map<String, Object> map = (Map<String, Object>)parent;
			map.put("child", new ArrayList<Map<String, Object>>());
		}
		
	}

	public static class MapTargetXmlSource extends AbstractNavTargetCallback {

		public MapTargetXmlSource(Object data) { super(data); }

		@Override
		public void onroot(Object rootObject, Object userObject) {
			Map<String, Object> root = (Map<String, Object>)rootObject;
			Map<String, Object> data = (Map<String, Object>)userObject;
			
			root.put("@key", (String)data.get("key"));
			root.put("@value", (String)data.get("value"));
		}
		
		@Override
		public Object onnode(Object parentObject, Object userObject) {
			Map<String, Object> parent = (Map<String, Object>)parentObject;
			Map<String, Object> data = (Map<String, Object>)userObject;
			
			Map<String, Object> child = new HashMap<String, Object>();
			child.put("@key", (String)data.get("key"));
			child.put("@value", (String)data.get("value"));
			
			((List<Map<String, Object>>)parent.get("child")).add(child);
			return child;
		}

		@Override
		public void onchild(Object parent) {
			Map<String, Object> map = (Map<String, Object>)parent;
			map.put("child", new ArrayList<Map<String, Object>>());
		}
		
	}
	
}
